<?php

//<editor-fold defaultstate="collapsed" desc="License & Copyright">
/*
 * @license
 */

/**
 *
 * @author Jevgeni Geimanen (jevgeni.geimanen@gmail.com)
 */
//</editor-fold>
class SCSelectFormField extends FormField {

    //<editor-fold defaultstate="collapsed" desc="Multiple">
    /**
     * @var boolean
     */
    private $_Multiple = null;

    /**
     *
     * @return boolean
     */
    public function getMultiple() {
        return $this->_Multiple;
    }

    /**
     *
     * @param boolean $multiple
     * @return SCSelectFormField 
     */
    public function setMultiple($multiple) {
        $this->_Multiple = $multiple;
        return $this;
    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Overrides">
    protected function CollectMainTagAttributes(&$attributes) {
        if ($this->getMultiple())
            $attributes['multiple'] = 1;
        parent::CollectMainTagAttributes($attributes);
    }

    public function getMainTagName() {
        return 'select';
    }

    protected function getHasContents() {
        return true;
    }

    private $_Contents;

    protected function RenderContents() {
        if ($this->_Contents == null) {
            $this->_Contents = array();
            if (is_array($value = $this->getOptions()) && count($value)) {
                $default = $this->getValue();
                foreach ($value as $key => $val) {
                    if ($key!=null && in_array($key, $default)) {
                        $this->_Contents[] = <<<HTML
<option value="{$key}" selected>{$val}</option>
HTML;
                    } else {
                        $this->_Contents[] = <<<HTML
<option value="{$key}">{$val}</option>
HTML;
                    }
                }
            }
            $this->_Contents = join("\n", $this->_Contents);
        }
        return $this->_Contents;
    }

    public function getFieldType() {
        return self::TYPE_SELECT;
    }

    protected function getIsMultiValue() {
        return true;
    }

    //</editor-fold>
}

?>
